#include "view_sports.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "sports_mode.h"
#include "motor.h"
#include "time/view_times.h"
#include "language_text.h"
#include "app_trace.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_sports_base_display(void)
{
#ifdef CUSTOM_UI
    int const temp_index_start = 50;
    int custom_ui_index = 0;
    for (int i = 0; i < SPORTS_BACK_COUNT; i++)
    {
        disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_sports_back_start + i, temp_index_start + custom_ui_index, -1, -1);
        ++custom_ui_index;
    }
#if SPORTS_GIF_COUNT > 0
    disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_sports_gif_start + disp_ctrl.auto_refresh_count % HEART_RATE_GIF_COUNT,
                                temp_index_start + custom_ui_index, -1, -1);
    ++custom_ui_index;
#endif
    int x = 89;
    int y = 174;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 1;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {

        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0 + custom_ui_index, x, y, FILE_IMG_2002_main_page_text);
    }
    //    bool ret = disp_show_localize(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_string_key_sports_text, temp_index_start + custom_ui_index);
    // if (!ret)
    // {
    //     _disp_show_flash_img(DISP_LFS_FILE_INDEX_0 + custom_ui_index, 76, 177, FILE_IMG_3002_main_text);
    // }
    //++custom_ui_index;

#else
    int x = 70;
    int y = 44;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_2001_main_page_icon);
    x = 89;
    y = 174;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 1;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {

        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_2002_main_page_text);
    }
    show_button_up();
    show_button_right();
    show_button_down();
#endif
}

int get_sports_icon(void)
{
    sports_mode_mode_t mode = sports_mode_get_now_mode();
    switch (mode)
    {
#ifdef SPORTS_SWIM
    case sports_mode_mode_swim:
        return FILE_IMG_swim_icon;
#endif
    case sports_mode_mode_run_2:
        return FILE_IMG_2407_date_run;
    case sports_mode_mode_walk:
        return FILE_IMG_2408_date_walk;
    case sports_mode_mode_work_out:
        return FILE_IMG_2406_date_fitness;
    case sports_mode_mode_swing:
        return FILE_IMG_2405_date_swing;
    case sports_mode_mode_ride:
        return FILE_IMG_2404_date_cycl;
    case sports_mode_mode_basketball:
        return FILE_IMG_2412_basketball;
    case sports_mode_mode_football:
        return FILE_IMG_2411_football;
    default:
        return FILE_IMG_2407_date_run;
    }
}
void view_sports_result_1_display(void)
{
    uint8_t *detail_buffer = (uint8_t *)&disp_ctrl.com_data.i_temp[5];
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_sports_avg_heart_rate();
        disp_ctrl.com_data.i_temp[1] = sports_mode_get_sports_minimal_heart_rate();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_maximum_heart_rate();
        // 19 + 170
        // 19+(170-68*2)/2 = 36   start_x
        // 36 代表50
        // start y 183
        int const start_y = 183;
        disp_ctrl.com_data.i_temp[3] = sports_mode_get_detail_info((sports_mode_detail_t *)detail_buffer, SPORTS_MODE_DETAIL_CACHE_COUNT);
        // disp_ctrl.com_data.i_temp[3] = 68;
        // for (int i = 0; i < disp_ctrl.com_data.i_temp[3]; i++)
        // {
        //     detail_buffer[i] = 50 + i * 3;
        // }
        // change to y
        for (int i = 0; i < disp_ctrl.com_data.i_temp[3]; i++)
        {
            if (detail_buffer[i] != 0)
            {
                detail_buffer[i] = start_y - (detail_buffer[i] - 50) * 36 / 50;
            }
        }
    }
    int x = 0;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_sports_mode_result_2);
    x = 69;
    y = 193;
    disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[2], 14, num_N200_dig_12x24_TAB);
    x = 147;
    disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[1], 14, num_N200_dig_12x24_TAB);
    x = 110;
    y = 14;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[0], 14, num_N200_dig_12x24_TAB);

    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y + 12, FILE_IMG_2433_unit_bpm);
    int const start_x = 36;
    for (int i = 0; i < disp_ctrl.com_data.i_temp[3] - 1; i++)
    {
        if (detail_buffer[i] != 0 && detail_buffer[i + 1] != 0)
        {
            disp_draw_hr_line(start_x + i * 2, detail_buffer[i], start_x + (i + 1) * 2, detail_buffer[i + 1]);
        }
    }
    show_button_right();
    show_button_up();
    show_button_down();
}
void view_sports_result_0_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        sports_mode_mode_t mode = sports_mode_get_now_mode();
        switch (mode)
        {
        case sports_mode_mode_run_2:
        case sports_mode_mode_walk:
            disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_mileage();
            if (system_config_get_km_mi() != 0)
            {
                disp_ctrl.com_data.i_temp[4] *= 0.6213712;
            }
            break;
#ifdef SPORTS_SWIM
        case sports_mode_mode_swim:
#endif
        case sports_mode_mode_work_out:
        case sports_mode_mode_swing:
        case sports_mode_mode_ride:
        case sports_mode_mode_basketball:
        case sports_mode_mode_football:
            disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_calorie() / 1000;
            break;
        default:
            return;
        }
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_sports_start_time();
        disp_ctrl.com_data.i_temp[1] = sports_mode_get_sports_duration();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_minimal_heart_rate();
        disp_ctrl.com_data.i_temp[3] = sports_mode_get_sports_maximum_heart_rate();
    }
    int x = 0;
    int y = 62;
    for (x = 0; x < 240; x++)
    {
        disp_draw_point(x, y, 0x5acb);
    }
    y = 117;
    for (x = 0; x < 240; x++)
    {
        disp_draw_point(x, y, 0x5acb);
    }
    y = 173;
    for (x = 0; x < 240; x++)
    {
        disp_draw_point(x, y, 0x5acb);
    }
    sports_mode_mode_t mode = sports_mode_get_now_mode();
    int index = 0;
    switch (mode)
    {
    case sports_mode_mode_run_2:
    case sports_mode_mode_walk:
        index = FILE_IMG_2416_distance_icon; // 2415;
        {
            // int wide = 10 + 32 + 16 * get_num_digits(disp_ctrl.com_data.i_temp[4] / 1000);
            x = 96;
            y = 188;
            x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[4] / 1000, 14, num_N200_dig_12x24_TAB);
            x += 2;
            disp_draw_user_image(x, y + 20, num_N210_dig_4x4_dot);
            x += 8;
            x = disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[4] % 1000 / 10, 2, 14, num_N200_dig_12x24_TAB);
            y = 199;
            if (system_config_get_km_mi() != 0)
            {
                _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_2432_unit_mi);
            }
            else
            {
                _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_2431_unit_km);
            }
        }
        x = 61;
        y = 189 + 2;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, index);
        break;
#ifdef SPORTS_SWIM
    case sports_mode_mode_swim:
#endif
    case sports_mode_mode_work_out:
    case sports_mode_mode_swing:
    case sports_mode_mode_ride:
    case sports_mode_mode_basketball:
    case sports_mode_mode_football:
        index = FILE_IMG_2415_calories_icon;
        x = 98;
        y = 188;
        x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[4], 16, num_N300_dig_14x32_TAB);
        y = 199;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_2430_unit_kcal);

        x = 61;
        y = 189;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, index);
        break;
    }
    x = 111;
    y = 8;
    if (mode == sports_mode_mode_swing)
    {
        y -= 2;
    }
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, get_sports_icon());

    x = 61;
    y = 80;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_2414_time_icon);

    y = 136;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_2417_heartrate_icon);

    x = 95;
    y = 132;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[2], 14, num_N200_dig_12x24_TAB);
    disp_draw_rect_color(x, y + 12, x + 12, y + 13, RGB565_COLOR_WHITE);
    x += 14;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[3], 14, num_N200_dig_12x24_TAB);
    y = 146;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_2433_unit_bpm);

    show_button_up();
    show_button_right();
    show_button_down();

    int second = disp_ctrl.com_data.i_temp[1] % 60;
    int minute = disp_ctrl.com_data.i_temp[1] / 60 % 60;
    int hour = disp_ctrl.com_data.i_temp[1] / 60 / 60;

    x = 95;
    y = 78;
    x = disp_draw_user_image_num_with_digits(x, y, hour, 2, 14, num_N200_dig_12x24_TAB);
    disp_draw_user_image(x, y + 5, num_N213_pace_4x14_dot);
    x += 6;
    x = disp_draw_user_image_num_with_digits(x, y, minute, 2, 14, num_N200_dig_12x24_TAB);
    disp_draw_user_image(x, y + 5, num_N213_pace_4x14_dot);
    x += 6;
    x = disp_draw_user_image_num_with_digits(x, y, second, 2, 14, num_N200_dig_12x24_TAB);

    datetime_t time = utc_time_to_date_time(disp_ctrl.com_data.i_temp[0]);
    if (if_time_hour_system_12())
    {
        time.time.hour %= 12;
        if (time.time.hour == 0)
        {
            time.time.hour = 12;
        }
    }
    x = 67;
    y = 43;
    int font_interval = 1;
    int font_width = 7 + font_interval;
    x = disp_draw_user_image_num_with_digits(x, y, 2000 + time.date.year, 4, font_width, num_N101_dig_7x15_TAB);
    disp_draw_user_image(x, y + 12, num_N115_3x3_dot);
    x += 3 + font_interval;
    x = disp_draw_user_image_num_with_digits(x, y, time.date.month, 2, font_width, num_N101_dig_7x15_TAB);
    disp_draw_user_image(x, y + 12, num_N115_3x3_dot);
    x += 3 + font_interval;
    x = disp_draw_user_image_num_with_digits(x, y, time.date.day, 2, font_width, num_N101_dig_7x15_TAB);
    x += 10;
    x = disp_draw_user_image_num_with_digits(x, y, time.time.hour, 2, font_width, num_N101_dig_7x15_TAB);
    disp_draw_user_image(x, y + 3, num_N112_3x10_second_dot);
    x += 3 + font_interval;
    x = disp_draw_user_image_num_with_digits(x, y, time.time.minute, 2, font_width, num_N101_dig_7x15_TAB);
}

void view_sports_state_2_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
    }
    int x = 0;
    int y = 0;
    if (disp_ctrl.com_data.i_temp[0])
    {

        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_K101_music_back);
        x = 77;
        y = 189;
        if (disp_ctrl.com_data.uint8_temp[100])
        {
            y -= 4;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_k101_music_control_77_189);
        }
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_K106_notcon_back);
        x = 69;
        y = 189;
        if (disp_ctrl.com_data.uint8_temp[150])
        {
            y -= 3;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[151]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_K106_notcon_back_69_189);
        }
    }
}
void view_sports_state_3_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = battery_ctrl_get_level(battery_ctrl_get_percent());
        disp_ctrl.com_data.i_temp[2] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[5] = hr_if_band_wear();
        disp_ctrl.com_data.i_temp[6] = if_time_hour_system_12();
        if (disp_ctrl.com_data.i_temp[6])
        {
            disp_ctrl.com_data.i_temp[6] += (disp_ctrl.com_data.i_temp[3] >= 12); // 1=am,2=pm
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
    }
    int x = 0;
    int y = 0;
    if (!disp_ctrl.com_data.i_temp[5])
    {
        int x = 25;
        int y = 164;
        if (disp_ctrl.com_data.uint8_temp[100])
        {
            y -= 1;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_J120_watch_25_164);
        }
        x = 41;
        y = 49;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_J120_watch_41_49);
        return;
    }
    x = 73;
    y = 32;
    if (disp_ctrl.com_data.i_temp[0])
    {
        disp_draw_user_image(x, y, bmp_1101_connect);
    }
    else
    {
        disp_draw_user_image(x, y, bmp_1102_noconnect);
    }
    x = 142;
    y = 34;
    uint8_t battery_level = disp_ctrl.com_data.i_temp[1];
    if (disp_ctrl.com_data.i_temp[2])
    {
        disp_ctrl.auto_refresh_time = 500;
        // if (battery_level == 0)
        // {
        //     battery_level = disp_ctrl.auto_refresh_count % 2;
        // }
        // else if (battery_level < 4) //充电格
        // {
        //     battery_level = disp_ctrl.auto_refresh_count % (battery_level + 1) + 1;
        // }
        battery_level = disp_ctrl.auto_refresh_count % 5;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_1103_electricity_100 + 4 - battery_level);
    }
    else
    {
        disp_ctrl.auto_refresh_time = 5000;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_1103_electricity_100 + 4 - battery_level);
    }
    x = 46;
    y = 99;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_N500_0_25x59 + disp_ctrl.com_data.i_temp[3] / 10);
    x = 78;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_N500_0_25x59 + disp_ctrl.com_data.i_temp[3] % 10);
    x = 134;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_N500_0_25x59 + disp_ctrl.com_data.i_temp[4] / 10);
    x = 166;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_N500_0_25x59 + disp_ctrl.com_data.i_temp[4] % 10);
    x = 114;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_N510_second_9x59);

    // x = 213;
    // y = 91;
    // disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I201_btn_up);
    // y = 111;
    // disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I200_btn_start);
}
void view_sports_state_1_display(void)
{
    int level = 0;
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_duration();
        disp_ctrl.com_data.i_temp[3] = hr_if_band_wear();

        level = get_heart_rate_level(disp_ctrl.com_data.i_temp[1]);
        disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_heart_rate_level_seconds(level);
        if (level < 5)
        {
            disp_ctrl.com_data.i_temp[5] = sports_mode_get_sports_heart_rate_level_seconds(level + 1);
        }
        if (level > 1)
        {
            disp_ctrl.com_data.i_temp[6] = sports_mode_get_sports_heart_rate_level_seconds(level - 1);
        }
        disp_ctrl.auto_refresh_time = 1000;
    }
    if (!disp_ctrl.com_data.i_temp[3])
    {
        int x = 25;
        int y = 164;
        if (disp_ctrl.com_data.uint8_temp[100])
        {
            y -= 1;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_J120_watch_25_164);
        }
        x = 41;
        y = 49;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_J120_watch_41_49);
        return;
    }
    int x = 0;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_2434_heart);

    level = get_heart_rate_level(disp_ctrl.com_data.i_temp[1]);
    if (disp_ctrl.com_data.i_temp[1] != 0)
    {
        int index = FILE_IMG_UI_FILE_ENUM_END;
        switch (level)
        {
        case 1:
            x = 0;
            y = 90;
            index = FILE_IMG_2435_heart_interval_1_0_90;
            break;
        case 2:
            x = 4;
            y = 17;
            index = FILE_IMG_2436_heart_interval_2_4_17;
            break;
        case 3:
            x = 62;
            y = 0;
            index = FILE_IMG_2437_heart_interval_3_62_0;
            break;
        case 4:
            x = 178;
            y = 17;
            index = FILE_IMG_2438_heart_interval_4_178_17;
            break;
        case 5:
            x = 214;
            y = 92;
            index = FILE_IMG_2439_heart_interval_5_214_92;
            break;
        }
        if (index != FILE_IMG_UI_FILE_ENUM_END)
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, index);
        }
    }
    x = 12;
    y = 89;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_N424_heart_bottom);

    x = 78;
    y = 110;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_N423_heart_icon);

    x = 40;
    y = 105;

    if (level > 0)
    {
        //区间 0 不显示
        disp_draw_user_image_filter_color(x, y, num_N200_dig_12x24_TAB[level]);
    }

    x = 100;
    int font_width = 13;
    int minute = 0;
    int second = 0;
    int digits = 0;
    if (disp_ctrl.com_data.i_temp[1] == 0)
    {
        int second_x = 186;
        //心率显示 -- ，时间也显示
        if (disp_ctrl.auto_refresh_count % 2 == 0)
        {
            for (int i = 0; i < 12; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    disp_draw_point(x + i, y + 12 + j, RGB565_COLOR_WHITE);
                    disp_draw_point(x + 14 + i, y + 12 + j, RGB565_COLOR_WHITE);

                    disp_draw_point(second_x + i, y + 12 + j, RGB565_COLOR_WHITE);
                    disp_draw_point(second_x + 14 + i, y + 12 + j, RGB565_COLOR_WHITE);
                }
            }
        }
    }
    else
    {
        disp_draw_user_image_num_filter_color(x, y, disp_ctrl.com_data.i_temp[1], font_width, num_N200_dig_12x24_TAB);

        minute = disp_ctrl.com_data.i_temp[4] / 60;
        second = disp_ctrl.com_data.i_temp[4] % 60;
        if (disp_ctrl.loop_cnt == 0)
        {
            LOG("level %u %u:%u", disp_ctrl.com_data.i_temp[4], minute, second);
        }
        digits = get_num_digit(minute);

        x = 191 - digits * font_width;
        disp_draw_user_image_num_with_digits_filter_color(x, y, minute, digits, font_width, num_N200_dig_12x24_TAB);

        x = 197;
        disp_draw_user_image_num_with_digits_filter_color(x, y, second, 2, font_width, num_N200_dig_12x24_TAB);

        x = 191;
        y = 105;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_N423_heart_4x7_pace);
    }

    if (level < 5)
    {
        x = 41;
        y = 65;
        disp_draw_user_image_filter_color(x, y, num_N101_dig_7x15_TAB[level + 1]);
        x = 104;
        y = 68;
        disp_draw_user_image(x, y, bmp_N402_positive);
        x = 179;
        y = 65;
        disp_draw_user_image(x, y, num_N113_3x5_dot);

        font_width = 8;
        minute = disp_ctrl.com_data.i_temp[5] / 60;
        second = disp_ctrl.com_data.i_temp[5] % 60;
        if (disp_ctrl.loop_cnt == 0)
        {
            LOG("level +1 %u %u:%u", disp_ctrl.com_data.i_temp[5], minute, second);
        }
        digits = get_num_digit(minute);

        x = 178 - digits * font_width;
        disp_draw_user_image_num_with_digits_filter_color(x, y, minute, digits, font_width, num_N101_dig_7x15_TAB);

        x = 183;
        disp_draw_user_image_num_with_digits_filter_color(x, y, second, 2, font_width, num_N101_dig_7x15_TAB);

        int temp = get_target_heart_rate_level(disp_ctrl.com_data.i_temp[1], level, level + 1);
        x = 113;
        disp_draw_user_image_num(x, y, temp, font_width, num_N101_dig_7x15_TAB);
    }
    if (level > 1)
    {

        x = 41;
        y = 156;
        disp_draw_user_image_filter_color(x, y, num_N101_dig_7x15_TAB[level - 1]);
        x = 104;
        y = 163;
        disp_draw_user_image(x, y, bmp_N403_negative);
        x = 179;
        y = 156;
        disp_draw_user_image(x, y, num_N113_3x5_dot);

        font_width = 8;
        minute = disp_ctrl.com_data.i_temp[6] / 60;
        second = disp_ctrl.com_data.i_temp[6] % 60;
        if (disp_ctrl.loop_cnt == 0)
        {
            LOG("level -1 %u %u:%u", disp_ctrl.com_data.i_temp[6], minute, second);
        }
        digits = get_num_digit(minute);

        x = 178 - digits * font_width;
        disp_draw_user_image_num_with_digits_filter_color(x, y, minute, digits, font_width, num_N101_dig_7x15_TAB);

        x = 183;
        disp_draw_user_image_num_with_digits_filter_color(x, y, second, 2, font_width, num_N101_dig_7x15_TAB);

        int temp = get_target_heart_rate_level(disp_ctrl.com_data.i_temp[1], level, level - 1);
        x = 113;
        disp_draw_user_image_num(x, y, temp, font_width, num_N101_dig_7x15_TAB);
    }

    x = 109;
    y = 22;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_N404_logs_icon_small);

    second = disp_ctrl.com_data.i_temp[2] % 60;
    minute = disp_ctrl.com_data.i_temp[2] / 60 % 60;
    int hour = disp_ctrl.com_data.i_temp[2] / 60 / 60;
    y = 180;
    x = 56;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[hour / 10]);
    x = 74;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[hour % 10]);

    x = 104;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[minute / 10]);
    x = 122;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[minute % 10]);

    x = 154;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[second / 10]);
    x = 172;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[second % 10]);
    x = 93;
    disp_draw_user_image(x, y, num_N312_second_6x32);
    x = 142;
    disp_draw_user_image(x, y, num_N312_second_6x32);

    // x = 213;
    // y = 91;
    // disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I201_btn_up);
    // y = 111;
    // disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I200_btn_start);
    // y = 137;
    // disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I202_btn_down);
}
void view_sports_state_0_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_duration();
        disp_ctrl.com_data.i_temp[5] = hr_if_band_wear();
        sports_mode_mode_t mode = sports_mode_get_now_mode();
        switch (mode)
        {
        case sports_mode_mode_run_2:
        case sports_mode_mode_walk:
            disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_mileage();
            if (system_config_get_km_mi() != 0)
            {
                disp_ctrl.com_data.i_temp[4] *= 0.6213712;
            }
            break;
#ifdef SPORTS_SWIM
        case sports_mode_mode_swim:
#endif
        case sports_mode_mode_work_out:
        case sports_mode_mode_swing:
        case sports_mode_mode_ride:
        case sports_mode_mode_basketball:
        case sports_mode_mode_football:
            disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_calorie() / 1000;
            break;
        default:
            return;
        }
        disp_ctrl.auto_refresh_time = 500;
    }
    if (!disp_ctrl.com_data.i_temp[5])
    {
        int x = 25;
        int y = 164;
        if (disp_ctrl.com_data.uint8_temp[100])
        {
            y -= 1;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_J120_watch_25_164);
        }
        x = 41;
        y = 49;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_J120_watch_41_49);
        return;
    }
    int x = 0;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_2434_heart);

    x = 53 + 16;
    y = 113;
    // disp_ctrl.com_data.i_temp[1] = 60;
    if (disp_ctrl.com_data.i_temp[1] == 0)
    {
        if (disp_ctrl.auto_refresh_count % 2)
        {
            x = 53;
            y += 16;
            int x2 = x + 14;
            int y2 = y + 1;
            disp_draw_rect_color(x, y, x2, y2, RGB565_COLOR_WHITE);
            x += 16;
            x2 = x + 14;
            disp_draw_rect_color(x, y, x2, y2, RGB565_COLOR_WHITE);
        }
    }
    else
    {
        int level = get_heart_rate_level(disp_ctrl.com_data.i_temp[1]);
        disp_draw_user_image_num_mid_ignore(x, y, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[1], 16, num_N300_dig_14x32_TAB);
        int index = FILE_IMG_UI_FILE_ENUM_END;
        switch (level)
        {
        case 1:
            x = 0;
            y = 90;
            index = FILE_IMG_2435_heart_interval_1_0_90;
            break;
        case 2:
            x = 4;
            y = 17;
            index = FILE_IMG_2436_heart_interval_2_4_17;
            break;
        case 3:
            x = 62;
            y = 0;
            index = FILE_IMG_2437_heart_interval_3_62_0;
            break;
        case 4:
            x = 178;
            y = 17;
            index = FILE_IMG_2438_heart_interval_4_178_17;
            break;
        case 5:
            x = 214;
            y = 92;
            index = FILE_IMG_2439_heart_interval_5_214_92;
            break;
        }
        if (index != FILE_IMG_UI_FILE_ENUM_END)
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, index);
        }
    }

    x = 104;
    y = 21;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, get_sports_icon());

    x = 64;
    y = 77;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_2417_heartrate_icon);

    x = 120;

    for (y = 77; y <= 77 + 70; y++)
    {
        disp_draw_point(x, y, 0x5acb);
    }
    int index = 0;
    sports_mode_mode_t mode = sports_mode_get_now_mode();
    switch (mode)
    {
    case sports_mode_mode_run_2:
    case sports_mode_mode_walk:
        index = FILE_IMG_2416_distance_icon; // 2415;
        {
            int wide = 10 + 32 + 16 * get_num_digit(disp_ctrl.com_data.i_temp[4] / 1000);
            x = 120 + (96 - wide) / 2;
            y = 113;
            x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[4] / 1000, 16, num_N300_dig_14x32_TAB);
            x += 2;
            disp_draw_user_image(x, y, num_N311_point_6x32);
            x += 8;
            disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[4] % 1000 / 10, 2, 16, num_N300_dig_14x32_TAB);
        }
        break;
#ifdef SPORTS_SWIM
    case sports_mode_mode_swim:
#endif
    case sports_mode_mode_work_out:
    case sports_mode_mode_swing:
    case sports_mode_mode_ride:
    case sports_mode_mode_basketball:
    case sports_mode_mode_football:
        index = FILE_IMG_2415_calories_icon;
        x = 168;
        y = 113;
        disp_draw_user_image_num_mid_ignore(x, y, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[4], 16, num_N300_dig_14x32_TAB);
        break;
    }
    x = 159;
    y = 78;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, index);

    x = 110;
    y = 218;
    if (disp_ctrl.com_data.i_temp[0])
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_2423_connect_small_3);
    }
    else
    {
        img_gif_com_t const img_cfg_2 = {
            .gif_count = 3,
            .img[0].x = x,
            .img[0].y = y,
            .img[0].sn = FILE_IMG_2421_connect_small_1,
            .img[1].x = x,
            .img[1].y = y,
            .img[1].sn = FILE_IMG_2422_connect_small_2,
            .img[2].x = x,
            .img[2].y = y,
            .img[2].sn = FILE_IMG_2423_connect_small_3,
        };
        _disp_show_flash_img_gif_all(&img_cfg_2, DISP_LFS_FILE_INDEX_5);
    }
    int second = disp_ctrl.com_data.i_temp[2] % 60;
    int minute = disp_ctrl.com_data.i_temp[2] / 60 % 60;
    int hour = disp_ctrl.com_data.i_temp[2] / 60 / 60;
    if (second < 0)
    {
        second = 0;
    }
    y = 173;
    x = 56;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[hour / 10]);
    x = 74;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[hour % 10]);

    x = 104;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[minute / 10]);
    x = 122;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[minute % 10]);

    x = 154;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[second / 10]);
    x = 172;
    disp_draw_user_image(x, y, num_N300_dig_14x32_TAB[second % 10]);
    x = 93;
    disp_draw_user_image(x, y, num_N312_second_6x32);
    x = 142;
    disp_draw_user_image(x, y, num_N312_second_6x32);
}
void view_sports_ready_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        switch (view)
        {
        case view_sports_fitness_ready:
        case view_sports_swing_ready:
            //!< 是否使用app gps
            disp_ctrl.com_data.i_temp[3] = 0;
            break;
#ifdef SPORTS_SWIM
        case view_sports_swim_ready:
#endif
        case view_sports_runing_ready:
        case view_sports_walking_ready:
        case view_sports_cycling_ready:
        case view_sports_basketball_ready:
        case view_sports_football_ready:
            disp_ctrl.com_data.i_temp[3] = 1;
            break;
        default:
            return;
        }
        {
            datetime_t UIdatetime = DateSync();
            disp_ctrl.com_data.i_temp[0] = UIdatetime.time.hour;
            disp_ctrl.com_data.i_temp[1] = UIdatetime.time.minute;
            disp_ctrl.com_data.i_temp[2] = ble_if_connected();
            disp_ctrl.com_data.i_temp[4] = hr_get_hr_data();
            disp_ctrl.com_data.i_temp[5] = hr_if_band_wear();
            disp_ctrl.auto_refresh_time = 500;
        }
    }
    if (!disp_ctrl.com_data.i_temp[5])
    {
        int x = 25;
        int y = 164;
        if (disp_ctrl.com_data.uint8_temp[200])
        {
            y -= 1;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[201]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_J120_watch_25_164);
        }
        x = 41;
        y = 49;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_J120_watch_41_49);
        return;
    }
    int x = 108;
    int y = 60;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_2417_heartrate_icon);
    x = 66;
    y = 102;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_2304_text_box);
    x = 82;
    y = 119;

    if (disp_ctrl.com_data.uint8_temp[50])
    {
        y -= 1;
        disp_show_font_string_utf8_area(82, y, 210, y + 24, (char const *)(&disp_ctrl.com_data.uint8_temp[51]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_2305_txt_start);
    }

    x = 122;
    y = 13;
    disp_draw_user_image(x, y, num_N112_3x10_second_dot);
    y = 10;
    x = 105;
    disp_draw_user_image(x, y, num_N101_dig_7x15_TAB[disp_ctrl.com_data.i_temp[0] / 10]);
    x = 113;
    disp_draw_user_image(x, y, num_N101_dig_7x15_TAB[disp_ctrl.com_data.i_temp[0] % 10]);
    x = 125;
    disp_draw_user_image(x, y, num_N101_dig_7x15_TAB[disp_ctrl.com_data.i_temp[1] / 10]);
    x = 133;
    disp_draw_user_image(x, y, num_N101_dig_7x15_TAB[disp_ctrl.com_data.i_temp[1] % 10]);
    // show_button_right();
    x = 212;
    y = 123;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I200_btn_start);

    if (disp_ctrl.com_data.i_temp[3])
    {
        x = 48;
        if (disp_ctrl.com_data.i_temp[2])
        {

            y = 173;
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_2427_connect_big_3);
            y += 9;
            x += 39;
            if (disp_ctrl.com_data.uint8_temp[100])
            {
                y -= 5;
                // disp_show_font_string_utf8(x, y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
                disp_show_font_string_utf8_area(x, y, 204, y + 24, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
            }
            else
            {
                _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_2428_connection_text);
            }
        }
        else
        {
            y = 173;
            img_gif_com_t const img_cfg_2 = {
                .gif_count = 3,
                .img[0].x = x,
                .img[0].y = y,
                .img[0].sn = FILE_IMG_2425_connect_big_1,
                .img[1].x = x,
                .img[1].y = y,
                .img[1].sn = FILE_IMG_2426_connect_big_2,
                .img[2].x = x,
                .img[2].y = y,
                .img[2].sn = FILE_IMG_2427_connect_big_3,
            };
            _disp_show_flash_img_gif_all(&img_cfg_2, DISP_LFS_FILE_INDEX_3);
            y += 9;
            x += 39;
            if (disp_ctrl.com_data.uint8_temp[150])
            {
                y -= 4;
                // disp_show_font_string_utf8(x, y, (char const *)(&disp_ctrl.com_data.uint8_temp[151]), 0);
                disp_show_font_string_utf8_area(x, y, 204, y + 24, (char const *)(&disp_ctrl.com_data.uint8_temp[151]), 0);
            }
            else
            {
                _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_2429_connecting_text);
            }
        }
    }
    x = 141;
    y = 56;
    if (disp_ctrl.com_data.i_temp[4] == 0)
    {
        if (disp_ctrl.auto_refresh_count % 2)
        {
            int x1 = x;
            int y1 = y + 12;
            int x2 = x1 + 12;
            int y2 = y1 + 1;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);

            x1 += 14;
            x2 = x1 + 12;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
        }
    }
    else
    {
        disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[4], 14, num_N200_dig_12x24_TAB);
    }
}
void view_sports_level_1(int view)
{
    int x = 0;
    int y = 0;
    int icon_index = 0;
    int text_index = 0;
    int text_pic_index = 0;
    int text_offset_x = 0;
    int text_offset_y = 0;
    int text_str_offset_y = 0;
    switch (view)
    {
#ifdef SPORTS_SWIM
    case view_sports_swim:
        icon_index = FILE_IMG_2120_swim;
        text_index = FILE_IMG_2113_fitness_text;
        text_pic_index = FILE_IMG_2121_swim_text_13_15;
        text_offset_x = 13;
        text_offset_y = 15;
        text_str_offset_y = -3;
        x = 51;
        y = 168;
        break;
#endif
    case view_sports_runing:
        icon_index = FILE_IMG_2101_running;
        text_index = FILE_IMG_2109_running_text;
        text_pic_index = FILE_IMG_2109_running_text_50_14;
        text_offset_x = 50;
        text_offset_y = 14;
        text_str_offset_y = -3;
        x = 51;
        y = 168;
        break;
    case view_sports_walking:
        icon_index = FILE_IMG_2102_walk;
        text_index = FILE_IMG_2110_walking_text;
        text_pic_index = FILE_IMG_2110_walking_text_43_15;
        text_offset_x = 43;
        text_offset_y = 15;
        text_str_offset_y = -3;
        x = 49;
        y = 171;
        break;
    case view_sports_cycling:
        icon_index = FILE_IMG_2103_cycling;
        text_index = FILE_IMG_2111_cycling_text;
        text_pic_index = FILE_IMG_2111_cycling_text_38_12;
        text_offset_x = 38;
        text_offset_y = 12;
        text_str_offset_y = 0;
        x = 53;
        y = 168;
        break;
    case view_sports_fitness:
        icon_index = FILE_IMG_2105_fitness;
        text_index = FILE_IMG_2113_fitness_text;
        text_pic_index = FILE_IMG_2113_fitness_text_32_15;
        text_offset_x = 32;
        text_offset_y = 15;
        text_str_offset_y = -3;
        x = 48;
        y = 171;
        break;
    case view_sports_swing:
        icon_index = FILE_IMG_2104_swing_sport;
        text_index = FILE_IMG_2112_swing_text;
        text_pic_index = FILE_IMG_2112_swing_text_38_12;
        text_offset_x = 38;
        text_offset_y = 12;
        text_str_offset_y = 0;
        x = 48;
        y = 171;
        break;
    case view_sports_basketball:
        icon_index = FILE_IMG_2107_basketball;
        text_index = FILE_IMG_2112_swing_text;
        text_pic_index = FILE_IMG_2114_basketball_text_14_15;
        text_offset_x = 14;
        text_offset_y = 15;
        text_str_offset_y = -3;
        x = 48;
        y = 169;
        break;
    case view_sports_football:
        icon_index = FILE_IMG_2108_football;
        text_index = FILE_IMG_2112_swing_text;
        text_pic_index = FILE_IMG_2115_football_text_27_15;
        text_offset_x = 27;
        text_offset_y = 15;
        text_str_offset_y = -3;
        x = 49;
        y = 169;
        break;
    }
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 0, icon_index);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, text_index);
    x += text_offset_x;
    y += text_offset_y;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y += text_str_offset_y;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, text_pic_index);
    }
}

void view_sports_paused_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_sports_duration();
    }
    int x = 16;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_2306_txt_restore);
    x += 61;
    y += 26;
    if (disp_ctrl.com_data.uint8_temp[50])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[51]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_2306_txt_restore_61_26);
    }

    x = 84;
    y = 86;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_2308_txt_paused);
    }

    x = 14;
    y = 178;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_2307_txt_end);

    x += 85;
    y += 21;
    if (disp_ctrl.com_data.uint8_temp[150])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[151]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_2307_txt_end_85_21);
    }

    x = 95;
    y = 124;
    disp_draw_user_image(x, y, num_N410_dig_sec_7x24_dot);
    x = 149;
    disp_draw_user_image(x, y, num_N410_dig_sec_7x24_dot);

    int hours = disp_ctrl.com_data.i_temp[0] / 60 / 60;
    int minute = disp_ctrl.com_data.i_temp[0] / 60 % 60;
    int second = disp_ctrl.com_data.i_temp[0] % 60;

    x = 53;
    y = 115;
    disp_draw_user_image_num_with_digits(x, y, hours, 2, 20, num_N400_dig_19x42_TAB);
    x = 106;
    disp_draw_user_image_num_with_digits(x, y, minute, 2, 20, num_N400_dig_19x42_TAB);
    x = 160;
    disp_draw_user_image_num_with_digits(x, y, second, 2, 20, num_N400_dig_19x42_TAB);
}
void view_sports_error_short_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 3000;
    }
    if (disp_ctrl.auto_refresh_count == 0)
    {

        if (disp_ctrl.com_data.uint8_temp[100] && disp_ctrl.com_data.uint8_temp[150])
        {
            int x1 = 22;
            int x2 = x1 + 196 - 1;
            int y1 = 49;
            int y2 = 169;
            {
                uint8_t buff[120] = {0};
                int16_t align = align_type_right;
                utf8_string_to_unicode_string((const uint8_t *)&disp_ctrl.com_data.uint8_temp[101], buff, sizeof(buff));
                disp_show_font_string(x1, y1, x2, y2, (uint16_t *)buff, 0, &align);
            }
            disp_show_font_string_utf8_mid(170, (char const *)(&disp_ctrl.com_data.uint8_temp[151]), 0);
        }
        else
        {
            int x = 52;
            int y = 49;
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_2413_shortexercisetime);
        }
    }
    else
    {
        if (disp_ctrl.loop_cnt == 0)
        {
            display_show(view_sports, DISP_MODE_NORMAL);
        }
    }
}
bool is_sports_level_1(int view)
{
    switch (view)
    {
#ifdef SPORTS_SWIM
    case view_sports_swim:
#endif
    case view_sports_runing:
    case view_sports_walking:
    case view_sports_cycling:
    case view_sports_fitness:
    case view_sports_swing:
    case view_sports_basketball:
    case view_sports_football:
        return true;
    default:
        return false;
    }
}
bool is_sports_ready_view(int view)
{
    switch (view)
    {
#ifdef SPORTS_SWIM
    case view_sports_swim_ready:
#endif
    case view_sports_runing_ready:
    case view_sports_walking_ready:
    case view_sports_fitness_ready:
    case view_sports_swing_ready:
    case view_sports_cycling_ready:
    case view_sports_basketball_ready:
    case view_sports_football_ready:
        return true;
    default:
        return false;
    }
}
static void sports_start(int view)
{
    sports_mode_mode_t mode = sports_mode_mode_invalid;
    switch (view)
    {
#ifdef SPORTS_SWIM
    case view_sports_swim_ready:
        mode = sports_mode_mode_swim;
        break;
#endif
    case view_sports_runing_ready:
        mode = sports_mode_mode_run_2;
        break;
    case view_sports_walking_ready:
        mode = sports_mode_mode_walk;
        break;
    case view_sports_fitness_ready:
        mode = sports_mode_mode_work_out;
        break;
    case view_sports_swing_ready:
        mode = sports_mode_mode_swing;
        break;
    case view_sports_cycling_ready:
        mode = sports_mode_mode_ride;
        break;
    case view_sports_basketball_ready:
        mode = sports_mode_mode_basketball;
        break;
    case view_sports_football_ready:
        mode = sports_mode_mode_football;
        break;
    default:
        Ensures(false);
    }
    sports_mode_start(mode);
    display_show(view_sports_state_0, DISP_MODE_NORMAL);
}
int32_t view_sports_display(int32_t view)
{
    Expects(view >= view_sports);
    Expects(view < view_sports + VIEW_LEVEL_0_SUB_COUNT);
    if (view == view_sports_base)
    {
        view_sports_base_display();
        return 0;
    }
    if (is_sports_level_1(view))
    {
        view_sports_level_1(view);
        return 0;
    }
    if (is_sports_ready_view(view))
    {
        view_sports_ready_display(view);
        return 0;
    }
    switch (view)
    {
    case view_sports_state_0:
        view_sports_state_0_display();
        break;
    case view_sports_state_1:
        view_sports_state_1_display();
        break;
    case view_sports_state_2:
        view_sports_state_2_display();
        break;
    case view_sports_state_3:
        view_sports_state_3_display();
        break;
    case view_sports_result_0:
        view_sports_result_0_display();
        break;
    case view_sports_result_1:
        view_sports_result_1_display();
        break;
    case view_sports_paused:
        view_sports_paused_display();
        break;
    case view_sports_error_short:
        view_sports_error_short_display();
        break;
    }
    return 0;
}
view_event_result_t view_sports_event(int32_t view, event_param_t param)
{
    Expects(view >= view_sports);
    Expects(view < view_sports + VIEW_LEVEL_0_SUB_COUNT);

    switch (param.event)
    {
    case event_view_entry:
    {
        if (is_sports_ready_view(view))
        {
            disp_ctrl.auto_close_delay = 30 * 1000;
            hr_test_start(HR_CTRL_TYPE_HR_TEST);
            uint8_t *buff = &disp_ctrl.com_data.uint8_temp[51];
            uint32_t buff_len = &disp_ctrl.com_data.uint8_temp[100] - buff;
            disp_ctrl.com_data.uint8_temp[50] = get_language_text(language_text_index_Start, buff, buff_len);
            buff = &disp_ctrl.com_data.uint8_temp[101];
            buff_len = &disp_ctrl.com_data.uint8_temp[150] - buff;
            disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Connected, buff, buff_len);
            buff = &disp_ctrl.com_data.uint8_temp[151];
            buff_len = &disp_ctrl.com_data.uint8_temp[200] - buff;
            disp_ctrl.com_data.uint8_temp[150] = get_language_text(language_text_index_Connecting, buff, buff_len);
            buff = &disp_ctrl.com_data.uint8_temp[201];
            buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
            disp_ctrl.com_data.uint8_temp[200] = get_language_text(language_text_index_Not_wearing_a_watch, buff, buff_len);
            return view_event_result_handled;
        }
        else if (is_sports_level_1(view))
        {

            language_text_index_t text_index = (language_text_index_t)0;
            switch (view)
            {
#ifdef SPORTS_SWIM
            case view_sports_swim:
                text_index = language_text_index_Swimming;
                break;
#endif
            case view_sports_runing:
                text_index = language_text_index_Run;
                break;
            case view_sports_walking:
                text_index = language_text_index_Walk;
                break;
            case view_sports_fitness:
                text_index = language_text_index_Fitness;
                break;
            case view_sports_swing:
                text_index = language_text_index_Swing;
                break;
            case view_sports_cycling:
                text_index = language_text_index_Riding;
                break;
            case view_sports_basketball:
                text_index = language_text_index_Basketball;
                break;
            case view_sports_football:
                text_index = language_text_index_Football;
                break;
            }
            uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
            uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
            disp_ctrl.com_data.uint8_temp[100] = get_language_text(text_index, buff, buff_len);
            return view_event_result_handled;
        }
        else
        {
            switch (view)
            {
            case view_sports_error_short:
            {
                motor_start(100);
                uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
                uint32_t buff_len = &disp_ctrl.com_data.uint8_temp[150] - buff;
                disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Short_exercise_time, buff, buff_len);

                buff = &disp_ctrl.com_data.uint8_temp[151];
                buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
                disp_ctrl.com_data.uint8_temp[150] = get_language_text(language_text_index_Not_save, buff, buff_len);
                return view_event_result_handled;
            }
            case view_sports_base:
            {
                uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
                uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
                disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Sport, buff, buff_len);
                return view_event_result_handled;
            }
            case view_sports_state_0:
            case view_sports_state_1:
            case view_sports_state_3:
            {
                uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
                uint32_t buff_len = &disp_ctrl.com_data.uint8_temp[150] - buff;
                disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Not_wearing_a_watch, buff, buff_len);
                return view_event_result_handled;
            }
            case view_sports_state_2:
            {
                uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
                uint32_t buff_len = &disp_ctrl.com_data.uint8_temp[150] - buff;
                disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Music_control, buff, buff_len);

                buff = &disp_ctrl.com_data.uint8_temp[151];
                buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
                disp_ctrl.com_data.uint8_temp[150] = get_language_text(language_text_index_Not_connected, buff, buff_len);
                return view_event_result_handled;
            }
            case view_sports_paused:
            {
                uint8_t *buff = &disp_ctrl.com_data.uint8_temp[51];
                uint32_t buff_len = &disp_ctrl.com_data.uint8_temp[100] - buff;
                disp_ctrl.com_data.uint8_temp[50] = get_language_text(language_text_index_Restore, buff, buff_len);

                buff = &disp_ctrl.com_data.uint8_temp[101];
                buff_len = &disp_ctrl.com_data.uint8_temp[150] - buff;
                disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Paused, buff, buff_len);

                buff = &disp_ctrl.com_data.uint8_temp[151];
                buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
                disp_ctrl.com_data.uint8_temp[150] = get_language_text(language_text_index_End, buff, buff_len);
                return view_event_result_handled;
            }
            }
        }
        break;
    }
    case event_view_exit:
    {
        if (is_sports_ready_view(view))
        {
            hr_test_stop(HR_CTRL_TYPE_HR_TEST);
            return view_event_result_handled;
        }
        break;
    }
    case BUTTON_EVENT_TP_MOVE_UP:
    {
        switch (view)
        {
        case view_sports_paused:
            return view_event_result_handled;
        }
        break;
    }
    case BUTTON_EVENT_TP_MOVE_DOWN:
    {
        switch (view)
        {
        case view_sports_paused:
            return view_event_result_handled;
        }
        break;
    }
    case event_move_level_up:
    {
        switch (view)
        {
        case view_sports_state_0:
        case view_sports_state_1:
        case view_sports_state_2:
        case view_sports_state_3:
            return view_event_result_handled;
        case view_sports_paused:
            return view_event_result_handled;
        case view_sports_error_short:
        case view_sports_result_0:
        case view_sports_result_1:
            display_show(view_sports, DISP_MODE_NORMAL);
            return view_event_result_handled;
        }
        break;
    }
    case event_move_level_down:
    {
        if (is_sports_ready_view(view))
        {
            sports_start(view);
            return view_event_result_handled;
        }
        switch (view)
        {
        case view_sports_state_0:
        case view_sports_state_1:
        case view_sports_state_2:
        case view_sports_state_3:
            sports_mode_pause();
            display_show(view_sports_paused, DISP_MODE_NORMAL);
            return view_event_result_handled;
        case view_sports_paused:
            return view_event_result_handled;
        case view_sports_error_short:
        case view_sports_result_0:
        case view_sports_result_1:
            display_show(view_sports, DISP_MODE_NORMAL);
            return view_event_result_handled;

            // break;
        }
        break;
    }
    case event_move_previous_view:
    {
        switch (view)
        {
        case view_sports_level_1_begin:
            display_show(view_sports_level_1_end - VIEW_LEVEL_1_SUB_COUNT, DISP_MODE_UP_MOVE);
            return view_event_result_handled;
        case view_sports_paused:
            sports_mode_resume();
            display_show(view_sports_state_0, DISP_MODE_NORMAL);
            return view_event_result_handled;
        case view_sports_error_short:
            return view_event_result_handled;
        case view_sports_result_0:
            display_show(view_sports_result_1, DISP_MODE_UP_MOVE);
            return view_event_result_handled;
        case view_sports_state_3:
            if (ble_if_connected())
            {
                display_show(view_sports_state_2, DISP_MODE_UP_MOVE);
            }
            else
            {
                display_show(view_sports_state_1, DISP_MODE_UP_MOVE);
            }
            return view_event_result_handled;
        case view_sports_state_0:
            display_show(view_sports_state_3, DISP_MODE_UP_MOVE);
            return view_event_result_handled;
        }
        break;
    }
    case event_move_next_view:
    {

        if (is_sports_ready_view(view))
        {
            return view_event_result_handled;
        }
        switch (view)
        {
        case (view_sports_level_1_end - VIEW_LEVEL_1_SUB_COUNT):
            display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
            return view_event_result_handled;
        case view_sports_state_1:
            if (ble_if_connected())
            {
                display_show(view_sports_state_2, DISP_MODE_DOWN_MOVE);
            }
            else
            {
                display_show(view_sports_state_3, DISP_MODE_DOWN_MOVE);
            }
            return view_event_result_handled;
        case view_sports_state_3:
            display_show(view_sports_state_0, DISP_MODE_DOWN_MOVE);
            return view_event_result_handled;
        case view_sports_result_1:
            display_show(view_sports_result_0, DISP_MODE_DOWN_MOVE);
            return view_event_result_handled;
        case view_sports_error_short:
            return view_event_result_handled;
        case view_sports_paused:
            sports_mode_stop();
            return view_event_result_handled;
        }
        break;
    } //!< case event_move_next_view
    case BUTTON_EVENT_TP_TAP:
    {
        if (is_sports_ready_view(view))
        {
            sports_start(view);
            return view_event_result_handled;
        }
        else
        {
            switch (view)
            {
            case view_sports_state_2:
            {
                if (param.x < 80)
                {
                    music_ctrl_action(music_ctrl_action_previous);
                }
                else if (param.x < 160)
                {
                    music_ctrl_action(music_ctrl_action_start_stop);
                }
                else
                {
                    music_ctrl_action(music_ctrl_action_next);
                }
                return view_event_result_handled;
            }
            case view_sports_paused:
                if (param.y <= 120)
                {
                    sports_mode_resume();
                    display_show(view_sports_state_0, DISP_MODE_NORMAL);
                }
                else
                {
                    sports_mode_stop();
                }
                return view_event_result_handled;
            }
        }
        break;
    }
    } //!< switch (param.event)

    return view_event_result_not_handled;
}
